package Forms;

import Controls.Plotter;
import Core.ALIGN;
import Core.ContextAction;
import Core.ContextMenu;
import Core.Control;
import Core.Form;
import Core.GM;
import Core.Key;
import MathCore.Operands.Variable;
import MathCore.Processor;
import MathCore.Real;
import java.util.Vector;

public class GraphShow extends Form
{
	private Plotter grapher;
	private ContextMenu menu = new ContextMenu(this);
	private ContextAction back, center, legend, backtoeditor;

	public GraphShow(Vector functions, Vector variables, Variable varForChange, Processor proc)
	{
		super(Form.THIN_PANEL);

		grapher = new Plotter(functions, variables, varForChange, proc, body.getAvailableW(), body.getAvailableH());
		
		body.addControl(grapher);

		setWindowText(GM.PANEL_LEFTSOFT,  "-");
		setWindowText(GM.PANEL_RIGHTSOFT, "+");
		setWindowText(GM.PANEL_CENTER,    "Menu");

		back = new ContextAction("Back");
		center = new ContextAction("Center");
		legend = new ContextAction("Add legend");
		backtoeditor = new ContextAction("Back to editor");

		menu.addAction(backtoeditor);
		menu.addAction(center);
		//menu.addAction(legend);
		menu.addAction(back);

		allowActionText = false;
		body.select(Control.UPPER);
		body.allowMovement = false;
	}

	public void onEvent(int event, Object param)
	{
		super.onEvent(event, param);

		switch(event)
		{
			case GM.CONTEXT_RELEASED:
				if(param == center) grapher.reset();
				else if(param == legend)
				{
					grapher.showLegend(!grapher.legendEnabled());
					if(grapher.legendEnabled()) legend.button.setCaption("Remove legend");
					else legend.button.setCaption("Show legend");
				}
				else if(param == backtoeditor) close();
				break;

			case GM.UPPER_FORM_CLOSED:
				if(param instanceof Output) close();
				break;
		}
	}

	public void processKey(int key, int state)
	{
		super.processKey(key, state);

		if(state != Key.RELEASED)
		switch(key)
		{
			case Key.LS_KEY:
				grapher.zoomOut();
				break;

			case Key.RS_KEY:
				grapher.zoomIn();
				break;

			case Key.PL_KEY:
			case '4':
				grapher.shiftX(-Plotter.ShiftStep);
				break;

			case Key.PR_KEY:
			case '6':
				grapher.shiftX(Plotter.ShiftStep);
				break;

			case Key.PU_KEY:
			case '2':
				grapher.shiftY(-Plotter.ShiftStep);
				break;

			case Key.PD_KEY:
			case '8':
				grapher.shiftY(Plotter.ShiftStep);
				break;
		}

		if(state == Key.RELEASED)
		switch(key)
		{
			case Key.STAR_KEY:
				close();
				break;

			case Key.P_KEY:
				menu.show(panel.getCoord(), ALIGN.LEFT | ALIGN.BOTTOM);
				break;
		}
	}


}
